www.gusucode.com > VC++写的C编译器源代码附设计文档-源码程序 > VC++写的C编译器源代码附设计文档-源码程序/code/C- Compiler/FunArgsCheck.cpp

    // FunArgsCheck.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "cminus.h"

#include "FunArgsCheck.h"

/*  *    CFunArgsCheck
    *    Construction & destruction
  *	* *
   ***   Programer: 陆晓春
    *    Date:		2004.05.20             */

CFunArgsCheck::CFunArgsCheck()
{
	first = last = NULL;
}

CFunArgsCheck::~CFunArgsCheck()
{
	if( first ) delete first;
}

/*  *    CFunArgsCheck
    *    help routines
  *	* *
   ***   Programer: 陆晓春
    *    Date:		2004.05.20             */

void CFunArgsCheck::deleteList()
{
	if( first ) { delete first; first = last = NULL; }
}

/*  *    CFunArgsCheck
    *    public functions
  *	* *
   ***   Programer: 陆晓春
    *    Date:		2004.05.20             */

// insert a kFunDec node into the list, plus its parameters
void CFunArgsCheck::fa_insert( CTreeNode* pNode )
{
	ASSERT( pNode->nodekind == kFunDec );

	FunDecListRec* temp = new FunDecListRec( pNode->szName, pNode->type );
	CTreeNode* p = pNode->child[0];
	if( p ) {
		temp->params = new ParamListRec( p->type, p->bArray );
		temp->count++;

		ParamListRec* l = temp->params;
		while( p->sibling ) {
			p = p->sibling;
			l->next = new ParamListRec( p->type, p->bArray );
			l = l->next;
			temp->count++;
		}
	}
	if( first == NULL ) first = last = temp;
	else { last->next = temp; last = last->next; }
}

// check if a function call's arguments match its declaration parameters
// -1:	not found
// -2:  type not match
// -3:  match
// else count not match, return the declaration parameter count
int CFunArgsCheck::fa_check( CTreeNode* pNode )
{
	ASSERT( pNode->nodekind == kStmt && pNode->kind.stmt == kCall );

	FunDecListRec* l = first;
	while( l && l->name != pNode->szName ) l = l->next;
	if( l == NULL ) return -1;
	ParamListRec* p = l->params;
	CTreeNode* t = pNode->child[0];
	while( p && t ) 
		if( (p->type == t->type && p->bArray == t->bArray) ||
			(t->nodekind == kExp && t->kind.exp == kConst &&
			(t->type == _NUM || t->type == _CHARACTER))) {
			p = p->next; t = t->sibling;
		} else /* type not match */ return -2;
	if( p || t ) // count not match
		return l->count;
	return -3;
}